/*
 * @Author: 0x9DEFA478
 * @Date: 2024-08-04 15:13:58
 * @LastEditTime: 2024-08-06 21:33:08
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_TS_Queue.h"
#include "../LL/H_TS_LL.h"




void H_TS_Queue_Init(H_TS_Queue* Queue,void** Data,Hsize Size){

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_Init():空的句柄");
  oH_TS_Assert_HandleValid(Data==NULL,"H_TS:H_TS_Queue_Init():空的数据指针");
  oH_TS_Assert_ParamValid(Size<=1,"H_TS:H_TS_Queue_Init():错误的大小");

  H_TS_Semaphore_Init(&Queue->Semaphore,1,0);

  Queue->ReadIndex=0;
  Queue->WriteIndex=0;
  Queue->Size=Size;
  Queue->Data=Data;

}

int H_TS_Queue_Deinit(H_TS_Queue* Queue){

  oH_TS_Assert_HandleValid(Queue==NULL,"H_TS:H_TS_Queue_Deinit():空的句柄");

  if(0!=H_TS_Semaphore_Deinit(&Queue->Semaphore)){
    oH_TS_Crash("H_TS:H_TS_Queue_Deinit():子部件被占用");
    for(;;){
    }
  }

  return 0;
}

H_TS_Queue* new_H_TS_Queue(Hsize Size){
  H_TS_Queue* r;

  oH_TS_Assert_ParamValid(Size<=1,"H_TS:new_H_TS_Queue():错误的大小");

  oH_TS_LL_SchedulerSuspend();
  r=H_TS_LL_Core_Instance.Memory.Malloc(sizeof(H_TS_Queue)+Size*sizeof(void*));
  oH_TS_LL_ResumeSchedulerAuto();
  if(r==NULL){
    return r;
  }

  H_TS_Queue_Init(r,(void**)&r[1],Size);

  return r;

}

int delete_H_H_TS_Queue(H_TS_Queue* Queue){

  if(0!=H_TS_Queue_Deinit(Queue)){
    return -1;
  }

  oH_TS_LL_SchedulerSuspend();
  H_TS_LL_Core_Instance.Memory.Free(Queue);
  oH_TS_LL_ResumeSchedulerAuto();

  return 0;
}



